//
//  LoginRegisterViewController.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/14.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "LoginRegisterViewController.h"
#import "MainViewAndNoReadViewController.h"
#import "DrawerViewController.h"
#import "RootViewController.h"

@interface LoginRegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *userName;
@property (nonatomic, strong) UITextField *passWord;


@end

@implementation LoginRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    
}

- (void)remberPassWord{
    _userName.text = [UserInfo getUserName];
    _passWord.text = [UserInfo getPassWord];

}

- (void)setUI{
    self.view.backgroundColor = [UIColor colorWithWhite:0.918 alpha:1.000];
    
    CGSize imageSize ;
    if (IS_IPHONE_4) {
        imageSize = CGSizeMake(70, 70);
    }else{
        imageSize = LRLCGSizeMake(70,70);
    }
    
    UIImageView *headImage = [[UIImageView alloc]init];
    headImage.image = [UIImage imageNamed:@"lsLittleLogo"];
    headImage.backgroundColor = [UIColor clearColor];
    [self.view addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(KSHeight/9);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(imageSize);
    }];
    headImage.layer.cornerRadius = 35*FitSize;
    headImage.layer.masksToBounds = YES;
    
    float fontSize = 15;
    //车公司
    _userName = [[UITextField alloc]init];
    _userName.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_userName];
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImage.mas_bottom).offset(30);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    _userName.textAlignment = NSTextAlignmentCenter;
    _userName.delegate = self;
    _userName.clearButtonMode = YES;

    _userName.font = [UIFont systemFontOfSize:fontSize];
    _userName.placeholder = @"账户";
    
    //密码
    _passWord = [[UITextField alloc]init];
    _passWord.delegate = self;
    _passWord.backgroundColor = [UIColor whiteColor];
    _passWord.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_passWord];
    [_passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userName.mas_bottom).offset(1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
    _passWord.clearButtonMode = YES;
    _passWord.secureTextEntry = YES;
    _passWord.font = [UIFont systemFontOfSize:fontSize];
    _passWord.placeholder = @"密码";
    _passWord.keyboardType = UIKeyboardTypeNumberPad;
    
    //登录按钮
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    loginButton.backgroundColor = [UIColor colorWithRed:0.134 green:0.689 blue:1.000 alpha:1.000];
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_passWord.mas_bottom).offset(15);
        make.width.mas_equalTo(KSWidth/10*9);
        make.height.mas_equalTo(40);
        make.centerX.equalTo(self.view);
    }];
    loginButton.layer.cornerRadius = 5;
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    if ([UserInfo getUserName].length > 0) {
        [self remberPassWord];
    }
}

/**
 *  跳转到主界面，如果成功的话并给主界面的isLogin赋值，赋值为YES，否则return
 */
- (void)login{
    if (_userName.text.length == 0) {
        [XHToast showCenterWithText:@"请输入帐号"];
        return;
    }
    if (_passWord.text.length == 0) {
        [XHToast showCenterWithText:@"请输入密码"];
        return;
    }
    [LBProgressHUD showHUDto:self.view animated:YES];
    NSString *ashx = @"/Server/UserHandler.ashx";
    NSDictionary *dic = @{@"act":@"login",@"uname":_userName.text,@"upwd":_passWord.text};
    [httpPostRequest getDataByPostAshx:ashx and:dic withFinishBlock:^(id model, NSError *error) {
        [LBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (error == nil) {
            NSString *code = [NSString stringWithFormat:@"%@",[model objectForKey:@"code"]];
            if ([code isEqualToString:@"0"]) {
                NSDictionary *dic = model[@"data"][0];
                NSLog(@"用户信息%@",model);
                [UserInfo saveUserData:dic];
                [self pushToRootView];
            }else{
                [XHToast showCenterWithText:[model objectForKey:@"msg"]];
            }
        }else{
            [XHToast showBottomWithText:@"网络断开连接或者服务器异常"];
        }
    }];
}
//忘记密码响应事件，暂未添加保留
- (void)forgetPassWord{
    
    NSLog(@"忘记密码");
}
//跳转到主界面
- (void)pushToRootView{
    MainViewAndNoReadViewController *mainView = [[MainViewAndNoReadViewController alloc]init];
    DrawerViewController *drawView = [DrawerViewController new];
    RootViewController *rootView = [[RootViewController alloc]initWithCenterVC:mainView leftVC:drawView];
    [self presentViewController:rootView animated:YES completion:nil];
    
}
//用户名，密码输入框键盘失去响应
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_passWord resignFirstResponder];
    [_userName resignFirstResponder];
}




@end
